import { VxeGlobalRendererHandles } from "../../../../../types/all";
import XEUtils from "xe-utils";
import { createCellRender, createEditDateRender, createExportMethod, createFormItemRender, getComponent, getCellEditFilterProps, getFilterOns, handleConfirmFilter, equalDaterange } from "./common";
import { dayjs } from "element-plus";
import { h } from "vue";
export function getDatePickerCellValue(renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderCellParams | VxeGlobalRendererHandles.ExportMethodParams, defaultFormat: string) {
    const { props = {} } = renderOpts;
    const { row, column } = params;
    let cellValue = XEUtils.get(row, column.field as string);
    if (cellValue) {
        cellValue = dayjs(cellValue).format(props.format || defaultFormat);
    }
    return cellValue;
}

export default {
    renderEdit: createEditDateRender(
        {},
        () => {
            return [];
        },
        (renderOpts) => {
            const { props = {} } = renderOpts;
            return { format: props.format || "YYYY-MM-DD" };
        }
    ),
    renderCell: createCellRender(getDatePickerCellValue, () => {
        return ["YYYY-MM-DD"];
    }),
    renderFilter(renderOpts: VxeGlobalRendererHandles.RenderFilterOptions, params: VxeGlobalRendererHandles.RenderFilterParams) {
        const { column } = params;
        const { name, attrs }: any = renderOpts;
        return [
            h(
                "div",
                {
                    class: "vxe-table--filter-element-wrapper",
                },
                column.filters.map((option: any, oIndex: any) => {
                    const optionValue = option.data;
                    return h(getComponent(name), {
                        key: oIndex,
                        ...attrs,
                        ...getCellEditFilterProps(renderOpts, params, optionValue),
                        ...getFilterOns(renderOpts, params, option, () => {
                            // 处理 change 事件相关逻辑
                            handleConfirmFilter(params, !!option.data, option);
                        }),
                    });
                })
            ),
        ];
    },
    renderItemContent: createFormItemRender(),
    exportMethod: createExportMethod(getDatePickerCellValue, () => {
        return ["YYYY-MM-DD"];
    }),
    defaultFilterMethod(params: any) {
        const { option, row, column } = params;
        const { data } = option;
        const { filterRender: renderOpts } = column;
        const { props = {} } = renderOpts;
        const cellValue = XEUtils.get(row, column.field);
        if (data) {
            switch (props.type) {
                case "daterange":
                    return equalDaterange(cellValue, data, props, "YYYY-MM-DD");
                case "datetimerange":
                    return equalDaterange(cellValue, data, props, "YYYY-MM-DD HH:ss:mm");
                case "monthrange":
                    return equalDaterange(cellValue, data, props, "YYYY-MM");
                default:
                    return cellValue === data;
            }
        }
        return false;
    },
};
